package top.callname.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import top.callname.bean.ClassBean;
import top.callname.bean.ClassPageList;
import top.callname.bean.ConstanstBean;
import top.callname.bean.RspInfoBean;
import top.callname.dao.AdminDao;
import top.callname.dao.ClassDao;
import top.callname.dao.StudentDao;
import top.callname.service.ClassService;

public class ClassServiceImpl implements ClassService{

	private ClassDao classDao = new ClassDao();
	private StudentDao studentDao = new StudentDao();
	private AdminDao adminDao = new AdminDao();
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	public List<ClassBean> getList(Integer pid) {
		return classDao.selectByPid(pid);
	}

	@Override
	public ClassBean getByCid(Integer cid) {
		return classDao.selectByCid(cid);
	}

	@Override
	public ClassPageList getClassByPage(int pageNow) {
		return classDao.selectByPage(pageNow);
	}
	
	public List<ClassBean> getAll() {
		return classDao.selectAll();
	}

	@Override
	public RspInfoBean deleteClass(Integer cid) {
		RspInfoBean rsp = new RspInfoBean();
		//删除学生
		boolean f1 = studentDao.deleteByCid(cid);
		logger.debug("删除学生："+f1);
		//删除关系
		boolean f2 = adminDao.deleteFromAdminClassByCid(cid);
		logger.debug("删除关系："+f2);
		//删除班级
		boolean f3 = classDao.deleteByCid(cid);
		logger.debug("删除班级："+f3);
		rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
		return rsp;
	}

	@Override
	public RspInfoBean addClass(ClassBean classBean) {
		RspInfoBean rsp = new RspInfoBean(); 
		if(classDao.insertClass(classBean)){
			 rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			 rsp.setRspDesc("成功添加班级！");
		 }else{
			 rsp.setRspCode(ConstanstBean.RSP_FAULT);
			 rsp.setRspDesc("创建失败！");
		 }
		return rsp;
	}

}
